﻿namespace LowCoding.Common
{
    public static class Formatter
    {
        #region 性别

        /// <summary>
        /// 性别
        /// </summary>
        /// <param name="sexbool"></param>
        /// <returns></returns>
        public static string GetSex(this Boolean? sexbool)
        {
            if (sexbool == true)
            {
                return "男";
            }
            else if (sexbool == false)
            {
                return "女";
            }
            else
            {
                return "";
            }
        }

        #endregion 性别

        #region 状态

        /// <summary>
        /// 状态
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public static string GetDisableStatus(this Boolean? status)
        {
            if (status == true)
            {
                return "启用";
            }
            else if (status == false)
            {
                return "禁用";
            }
            else
            {
                return "";
            }
        }

        #endregion 状态

        #region 婚姻状况

        /// <summary>
        /// 婚姻状况
        /// </summary>
        /// <param name="maritalstatus"></param>
        /// <returns></returns>
        public static string GetMaritalStatus(this Boolean? maritalstatus)
        {
            if (maritalstatus == true)
            {
                return "已婚";
            }
            else if (maritalstatus == false)
            {
                return "未婚";
            }
            else
            {
                return "";
            }
        }

        #endregion 婚姻状况

        #region 是否

        /// <summary>
        /// 是否
        /// </summary>
        /// <param name="nature"></param>
        /// <returns></returns>
        public static string GetTrueFalse(this Boolean? nature)
        {
            return nature == true ? "是" : "否";
        }

        #endregion 是否

        #region decimal保留指定位数小数 不四舍五入

        /// <summary>
        /// decimal保留指定位数小数 不四舍五入
        /// </summary>
        /// <param name="num">原始数量</param>
        /// <param name="scale">保留小数位数</param>
        /// <returns>截取指定小数位数后的数量字符串</returns>
        public static decimal DecimalRound(decimal num, int scale)
        {
            string numToString = num.ToString();

            int index = numToString.IndexOf(".");
            int length = numToString.Length;
            if (index != -1)
            {
                if (scale > 0)
                {
                    return Convert.ToDecimal(string.Format("{0}.{1}",
                    numToString.Substring(0, index),
                    numToString.Substring(index + 1, Math.Min(length - index - 1, scale))));
                }
                return Convert.ToDecimal(numToString.Substring(0, index));
            }
            else
            {
                return num;
            }
        }

        #endregion decimal保留指定位数小数 不四舍五入

        #region 根据数值四舍五入取整

        /// <summary>
        /// 根据数值四舍五入取整
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static decimal DecimalRound(decimal num)
        {
            return decimal.Round(num, MidpointRounding.AwayFromZero);
        }

        #endregion 根据数值四舍五入取整
    }
}